home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsII / Peano / main.c next >
C/C++ Source or Header  |  1992-06-16  |  3KB  |  145 lines

  1. /* main.c */
  2.  
  3. /* copyright Ken Musgrave */
  4. /* June 1986 */
  5.  
  6. /*
  7.  * creates an 8-bit, 2-D peano curve image file
  8.  * background is 0, curve segments range in value from 1 to 255
  9.  *
  10.  * NOTE: the peano curve can be n-dimensional, 
  11.  * this is just an example of the use of the
  12.  * routines in peano.c
  13.  */
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include "types.h"
  19.  
  20. char           *image_file="im_file";
  21.  
  22. unsigned char   fb[FB_SIZE + 1][FB_SIZE + 1];
  23. FILE           *outfile, *fopen();
  24. vector          coord, last_coord;
  25.  
  26.  
  27. main(argc, argv)
  28. int             argc;
  29. char           *argv[];
  30.  
  31. {
  32.     int             i, pos=0;
  33.  
  34.         /* get command-line arg */
  35.     if (argc != 2) {
  36.         fprintf(stderr, "usage: %s precision\n", argv[0]);
  37.         exit(0);
  38.     }
  39.     dimensions = 2;     /* the dimension is fixed in this example */
  40.     precision = atoi(argv[1]);
  41.     if (precision < 0 || precision > MAX_PRECISION - 1) {
  42.         fprintf(stderr, "%s: can't work with %d bits of precision!\n", argv[0], precision);
  43.         exit(-1);
  44.     }
  45.     if ((outfile = fopen(image_file, "w")) == NULL) {
  46.         fprintf(stderr, "Error: cannot open file %s\n", image_file);
  47.         exit(-1);
  48.     }
  49.     printf("%s: filling %d dimensions to %d bits of precision.\n",
  50.             argv[0], dimensions, precision);
  51.  
  52.         /* begin the mysterious & incomprehensible algorithm... */
  53.     for (i=0; i<dimensions; i++) {
  54.         bitmask[i] = 1 << (dimensions - i - 1);
  55.         bytemask |= 1 << i;
  56.         last_coord[i] = 0;
  57.     }
  58.  
  59.         /* run the mysterious & incomprehensible algorithm... */
  60.     recurse(coord, last_coord, (int) pow(2.0, (double) precision),
  61.             dimensions);
  62.  
  63.         /* inform user that execution of algorithm is complete */
  64.     fprintf(stderr, "Spewing...");
  65.  
  66.     for (i=FB_SIZE-1; i>=0; i--)
  67.         fwrite(fb[i], 1, FB_SIZE, outfile);
  68.  
  69.     fprintf(stderr, " done.");
  70.  
  71.     exit(0);
  72.  
  73. } /* main() */
  74.  
  75.  
  76. /*
  77.  * recursive routine to call "peano()"
  78.  */
  79. recurse(coord, last_coord, iterations, level)
  80. vector          coord, last_coord;
  81. int             iterations, level;
  82. {
  83.     int             i, x, y, index;
  84.     static int      n=0, scale, offset;
  85.  
  86.     if (level > 0)
  87.         for (i=0; i<iterations; i++)
  88.             recurse(coord, last_coord, iterations, level - 1);
  89.     else {
  90.             /* get x,y coord of position n on peano curve */
  91.         peano(coord, n++);
  92.         offset = ((int) pow(2.0, (double) precision));
  93.         scale = FB_SIZE / ((int) pow(2.0, (double) precision));
  94.         offset = scale / 2;
  95.             /* draw line between adjacent coords */
  96.         draw_line(scale * coord[1] + offset,
  97.             FB_SIZE - scale * coord[0] - offset,
  98.             scale * last_coord[1] + offset,
  99.             FB_SIZE - scale * last_coord[0] - offset,
  100.             n);
  101.  
  102.         /*
  103.          * x = scale*coord[1]+offset; y =
  104.          * FB_SIZE-scale*coord[0]-offset; index = n%256;
  105.          */
  106.  
  107.         for (i=0; i<precision; i++)
  108.             last_coord[i] = coord[i];
  109.     }
  110.  
  111. } /* recurse() */
  112.  
  113.  
  114. /* 
  115.  * draws horizontal and vertical lines into "fb" with color "index"
  116.  */
  117. draw_line(x1, y1, x2, y2, index)
  118. unsigned        x1, y1, x2, y2, index;
  119. {
  120.     int    tmp, i;
  121.  
  122.     index = index % 256;
  123.     if (index == 0)
  124.         index = 1;
  125.  
  126.     if (x1 != x2) {        /* have horizontal line */
  127.         if (x1 > x2) {
  128.             tmp = x1;
  129.             x1 = x2;
  130.             x2 = tmp;
  131.         }
  132.         for (i=x1; i<=x2; i++)
  133.             fb[y1][i] = (unsigned char) index;
  134.     } else {        /* vertical line */
  135.         if (y1 > y2) {
  136.             tmp = y1;
  137.             y1 = y2;
  138.             y2 = tmp;
  139.         }
  140.         for (i=y1; i<=y2; i++)
  141.             fb[i][x1] = (unsigned char) index;
  142.     }
  143. } /* draw_line() */
  144.  
  145.